<template>
  <div id="wrap" :style="{ fontSize:fontSize + 'px' }">
    <div class="card">
      <div @click="$emit('open', idx)" class="questionTitle" :class="{ hasAnswer : selectedAnswer !== '' }">
        <div class="title">
          <a>{{idx}}</a>
          <div>{{description}}</div>
          <i class="iconfont icon-xiangyou1" :class="{ iconOpenClass : isOpen }"></i>
        </div>
        <div v-if="selectedAnswer !== ''" class="selected">
          <i></i>
          <div>
            {{selectedAnswer}}
          </div>
          <i class="iconfont icon-yuanxingxuanzhongfill"></i>
        </div>
      </div>
      <div class="questionContent" style="background: rgb(237, 241, 245)" :class="{ openClass : isOpen }">
        <div v-for="(value, key, idx) in choice" @click="selected(value, key)" :key="key">
          <i></i>
          <a>
            {{value}}
          </a>
        </div>
      </div>
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'question-card',
  props: {
    choice: {
      default: {},
      type: Object
    },
    description: {
      default: '',
      type: String
    },
    idx: {
      default: '',
      type: String
    },
    isOpen: {
      default: false,
      type: Boolean
    }
  },
  data () {
    return {
      fontSize: 10,
      selectedAnswer: ''
    }
  },
  methods: {
    selected (val, key) {
      this.selectedAnswer = val
      let body = {
        idx: this.idx,
        key: key
      }
      this.$emit('selectedAnswer', body)
    }
  },
  created () {
    this.fontSize = this.fontSize * document.body.offsetWidth / 320
  }
}
</script>

<style lang="less" scoped>
* {
  display: flex;
  justify-content: center;
  align-items: center;
  color: #666;
  width: 100%;
}
.card {
  flex-direction: column;
  width: 100%;
  display: flex;
  flex: 1;
}
.questionTitle {
  height: 4.9em;
  background: white;
  flex-direction: column;
  flex: 1;
  border-bottom: 1px solid #E9E9E9;
  .title {
    justify-content: space-between;
    a {
      font-size: 1.2em !important;
      flex: 1;
    }
    div {
      font-size: 1.2em !important;
      flex: 8;
      display: flex;
      justify-content: flex-start
    }
    i {
      flex: 1;
      color: rgba(102,102,102,0.50);
      transition: all 0.3s;
    }
  }
  .selected {
    padding-top: 1em;
    height: 2em;
    i {
      flex: 1;
      color: #4D66FF;
      font-size: 1.5em;
    }
    div {
      flex: 9;
      font-size: 1.2em;
      justify-content: flex-start;
      color: #AAA;
    }
  }
}
.hasAnswer {
  height: 7em;
}
.questionContent {
  display: flex;
  overflow: hidden;
  flex-direction: column;
  transition: all 0.3s;
  max-height: 0px;
  min-height: 0px;
  padding-bottom: 0;
  justify-content: flex-start;
  transition: all ease-out 0.2s;
  div {
    display: flex;
    a {
      font-size: 1.2em;
      flex: 9;
      justify-content: flex-start;
      border-bottom: 1px solid #E9E9E9;
      padding: 0.5em 0;
    }
    i {
      flex: 1;
      display: flex;
    }
  }
}
.openClass {
  max-height: 200px;
  padding-bottom: 0.8em !important;
  transition: all ease-in 0.2s;
}
.iconOpenClass {
  transform:rotate(90deg)
}
</style>